/*
/*
 * @Description: 
 * @Author: 胡涛
 * @Date: 2023-10-12 23:00:40
 * @LastEditors: 胡涛
 * @LastEditTime: 2023-10-14 12:51:54
 */
import api from "@/api";
import { useAppStore } from "@/store";
import axios, { AxiosResponse } from "axios";
import { ListResultDto, PagedListResultDto } from "../types";
import {
  GetFileResourceInputDto,
  FileResourceDto,
  UploadFileResourceInputDto,
} from "./types";

export default {
  /**
   * 获取单条记录
   * @param id
   * @param data
   * @returns
   */
  get(id: string): Promise<FileResourceDto> {
    return api.get("/api/Platform/Resources/file/" + id);
  },
  /**
   * 获取列表
   * @param id
   * @param data
   * @returns
   */
  getList(
    data: Partial<GetFileResourceInputDto>
  ): Promise<PagedListResultDto<FileResourceDto>> {
    return api.get("/api/Platform/Resources/file", data);
  },
  /**
   * 获取列表（不分页）
   * @param id
   * @param data
   * @returns
   */
  getAllList(
    data: Partial<GetFileResourceInputDto>
  ): Promise<Array<FileResourceDto>> {
    return api.get("/api/Platform/Resources/file/all", data);
  },
  /**
   * 添加
   * @param id
   * @param data
   * @returns
   */
  create(data: Partial<FileResourceDto>): Promise<FileResourceDto> {
    return api.post("/api/Platform/Resources/file", data);
  },
  /**
   * 修改
   * @param id
   * @param data
   * @returns
   */
  update(id: string, data: Partial<FileResourceDto>): Promise<FileResourceDto> {
    return api.put("/api/Platform/Resources/file/" + id, data);
  },
  /**
   * 删除
   * @param id
   * @returns
   */
  delete(id: string): Promise<void> {
    return api.delete("/api/Platform/Resources/file/" + id);
  },
  /**
   * 保存附件
   * @param input
   * @returns
   */
  upload(
    input: UploadFileResourceInputDto
  ): Promise<AxiosResponse<FileResourceDto>> {
    let data = new FormData();
    let keys = Object.keys(input);
    keys.map((name) => {
      data.append(name, input[name]);
    });
    let token = useAppStore().token;
    return axios.request({
      method: "POST",
      url: "/api/Platform/Resources/file/upload",
      data,
      baseURL: "",
      headers: {
        "Content-Type": "multipart/form-data",
        Authorization: token,
      },
      timeout: 0,
    });
  },
};
